package fr.cpe.dp.projet2.rmi.service;

import fr.cpe.dp.projet2.controller.Step1MusicController;
import fr.cpe.dp.projet2.factory.GenericFactory;
import fr.cpe.dp.projet2.main.Step1App;
import fr.cpe.dp.projet2.model.music.IMusic;
import fr.cpe.dp.projet2.model.music.MusicType;
import fr.cpe.dp.projet2.model.musicBuilder.IMusicBuilder;
import fr.cpe.dp.projet2.model.musicPlayer.ConsoleMusicPlayer;
import fr.cpe.dp.projet2.model.musicPlayer.IMusicPlayer;
import fr.cpe.dp.projet2.rmi.IRmiMusicService;
import fr.cpe.dp.projet2.view.Step1MusicPlayerView;
import java.awt.HeadlessException;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.sound.midi.MidiUnavailableException;
import javax.swing.JFrame;

public class RmiStep1MusicService implements IRmiMusicService {
    @Override
    public JFrame getIHM() {
        try {
            GenericFactory<IMusic> musicFactory = new GenericFactory<>("fr.cpe.dp.projet2.model.music");
            GenericFactory<IMusicBuilder> musicBuilderFactory = new GenericFactory<>("fr.cpe.dp.projet2.model.musicBuilder");
            
            Map<String,IMusicBuilder> mapMusicBuilders = new HashMap<>();
            mapMusicBuilders.put("Algo aléatoire", musicBuilderFactory.createObject("RandomBuilder"));
            mapMusicBuilders.put("Algo d'une gamme", musicBuilderFactory.createObject("RangeBuilder"));
            
            Map<String,MusicType> mapMusicTypes = new HashMap<>();
            mapMusicTypes.put("Un fichier midi", MusicType.FILE_TYPE);
            mapMusicTypes.put("Générer une musique", MusicType.GENERATED);
            
            IMusicPlayer musicPlayer = new ConsoleMusicPlayer();
            Step1MusicPlayerView musicPlayerView = new Step1MusicPlayerView(mapMusicBuilders.keySet().toArray(new String[1]), mapMusicTypes.keySet().toArray(new String[1]));
            
            Step1MusicController step1MusicController = new Step1MusicController(
                    musicPlayerView,
                    musicFactory,
                    musicPlayer,
                    mapMusicBuilders,
                    mapMusicTypes
            );
            
            RmiStep1MusicServiceView rmiStep1MusicServiceView = new RmiStep1MusicServiceView(step1MusicController);
            rmiStep1MusicServiceView.play();
            return rmiStep1MusicServiceView;
        } catch (MidiUnavailableException | InstantiationException | IllegalAccessException | ClassNotFoundException ex) {
            Logger.getLogger(Step1App.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    
    public class RmiStep1MusicServiceView extends JFrame {
        Step1MusicController step1MusicController;
                
        public RmiStep1MusicServiceView(Step1MusicController step1MusicController) throws HeadlessException {
            super();
            this.step1MusicController = step1MusicController;
        }
        
        public void play() {
            step1MusicController.play();
        }
    }
}
